<?php
/**
 * Copyright (c) 2018-2019.
 *  This file is part of the moonpie production
 *  (c) johnzhang <875010341@qq.com>
 *  This source file is subject to the MIT license that is bundled
 *  with this source code in the file LICENSE.
 */

namespace moonpie\plugin\wechat_account\controller;


use app\common\service\traits\PluginControllerTrait;
use app\manager\controller\Base;
use moonpie\plugin\wechat_account\model\WechatAccount;
use moonpie\plugin\wechat_account\validate\WechatAccountValidator;

class Index extends Base
{
    use PluginControllerTrait;
    public function index()
    {
        $list = WechatAccount::where('status', WechatAccount::STATUS_VALID)->paginate(10);
        $this->assign([
            'pageTitle' => '公众号列表',
            'list' => $list,
            'add_url' => url('backend.basic_wechat_account_add'),
            'del_url' => url('backend.basic_wechat_account_delete'),
            'edit_uri' => 'backend.basic_wechat_account_edit',
            'manage_uri' => 'backend.wechat_account_manage',
        ]);
        return $this->fetch('#/index/index');
    }
    public function addAccount()
    {
        $record = new WechatAccount(['title' => '', 'gh_id' => '','token' => '',  'app_id' => '', 'secret' => '', 'encrypt_key' => '', 'type' => WechatAccount::ACCOUNT_TYPE_OFFICIAL_AUDIT, 'status' => WechatAccount::STATUS_VALID]);
        if ($this->request->isPost()) {
            $data = $this->request->post('resource/a', []);

            $validator = new WechatAccountValidator();
            $result = $validator->check($data);
            if ($result) {
                $result = false !== $record->save($data);
                if (!$result) {
                    $this->error('抱歉，保存数据出现错误');
                }
                $this->success('添加成功', url('backend.basic_wechat_account_config'));
            }
            $this->error($validator->getError());
        }
        $this->assign([
            'pageTitle' => '添加公众号',
            'need_code' => true,
            'resource' => $record,
            'valid_types' => WechatAccount::getValidTypes(),
        ]);
        return $this->fetch('#/index/add_account');
    }
    public function dropAccount()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id');
            $record = WechatAccount::get($id);
            if (!$record) $this->error('请求数据不存在');
            $result = $record->markAsDropped();
            if (!$result) $this->error($record->getError());
            $this->success('删除成功');
        }
        $this->error('请求不存在');
    }

    public function editAccount($id)
    {
        $record = WechatAccount::get($id);
        if (!$record) $this->error('指定的记录不存在');
        if ($this->request->isPost()) {
            $data = $this->request->post('resource/a', []);
            $validator = new WechatAccountValidator();
            $result = $validator->setExceptRecord($record)->scene('update')->check($data);
            if ($result) {
                $result = false !== $record->save($data);
                if (!$result) {
                    $this->error('抱歉，保存数据出现错误');
                }
                $this->success('修改成功', url('backend.basic_wechat_account_config'));
            }
            $this->error($validator->getError());
        }
        $this->assign([
            'pageTitle' => '编辑公众号',
            'resource' => $record,
            'need_code' => false,
            'valid_types' => WechatAccount::getValidTypes(),
        ]);
        return $this->fetch('#/index/add_account');
    }
}